package com.liuchuanzheng.lcz_wanandroid.module.home.adapter

import android.annotation.SuppressLint
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.isGone
import androidx.core.view.isVisible
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.liuchuanzheng.lcz_wanandroid.R
import com.liuchuanzheng.lcz_wanandroid.ext.htmlToSpanned
import com.liuchuanzheng.lcz_wanandroid.module.home.bean.Article


class ArticleAdapter(layoutResId: Int = R.layout.item_article) : BaseQuickAdapter<Article, BaseViewHolder>(layoutResId) {

    init {
        addChildClickViewIds(R.id.iv_collect)
    }

    @SuppressLint("SetTextI18n")
    override fun convert(holder: BaseViewHolder, item: Article) {
        holder.run {
            itemView.run {
                findViewById<TextView>(R.id.tv_author).text = when {
                    !item.author.isNullOrEmpty() -> {
                        item.author
                    }
                    !item.shareUser.isNullOrEmpty() -> {
                        item.shareUser
                    }
                    else -> context.getString(R.string.anonymous)
                }
                findViewById<TextView>(R.id.tv_top).isVisible = item.top
                findViewById<TextView>(R.id.tv_fresh).isVisible = item.fresh && !item.top
                findViewById<TextView>(R.id.tv_tag).visibility = if (item.tags.isNotEmpty()) {
                    findViewById<TextView>(R.id.tv_tag).text = item.tags[0].name
                    View.VISIBLE
                } else {
                    View.GONE
                }
                findViewById<TextView>(R.id.tv_chapter).text = when {
                    !item.superChapterName.isNullOrEmpty() && !item.chapterName.isNullOrEmpty() ->
                        "${item.superChapterName.htmlToSpanned()}/${item.chapterName.htmlToSpanned()}"
                    item.superChapterName.isNullOrEmpty() && !item.chapterName.isNullOrEmpty() ->
                        item.chapterName.htmlToSpanned()
                    !item.superChapterName.isNullOrEmpty() && item.chapterName.isNullOrEmpty() ->
                        item.superChapterName.htmlToSpanned()
                    else -> ""

                }
                findViewById<TextView>(R.id.tv_title).text = item.title.htmlToSpanned()
                findViewById<TextView>(R.id.tv_desc).text = item.desc.htmlToSpanned()
                findViewById<TextView>(R.id.tv_desc).isGone = item.desc.isNullOrEmpty()
                findViewById<TextView>(R.id.tv_time).text = item.niceDate
                findViewById<ImageView>(R.id.iv_collect).isSelected = item.collect
            }
        }
    }
}